package com.ruoyi.ade.llm.chunk.pattern;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternCondition {

    private Pattern firstPattern;
    private List<Pattern> patterns = null;

    public PatternCondition(String first, String ... strings){
        firstPattern = Pattern.compile(first);
        patterns = new ArrayList<>();
        if(strings != null)
        for(String s:strings){
            patterns.add(Pattern.compile(s));
        }
    }

    public List<String> match(String doc){
        List<String> results = new ArrayList<>();
        Matcher m = firstPattern.matcher(doc);
        String tmp = null;
        while(m.find()){
            tmp = m.group(1);
            results.add(tmp);

            System.out.println("first:"+firstPattern.pattern());
            System.out.println("first:"+tmp);
            System.out.println();
        }
        if(results.size()==0)  //如果首选正则未匹配  则继续往下匹配
        for(Pattern p:patterns){
            Matcher m1 = p.matcher(doc);
            while(m1.find()){
                tmp = m1.group(1);
                results.add(tmp);

                System.out.println(p.pattern());
                System.out.println(tmp);
                System.out.println();
            }
            if(results.size()>0) break;   //只要找到了结果就停止
        }
        return results;
    }



}
